package com.dongbao.dialog.view.component;

import com.dongbao.data.Datacenter;
import com.dongbao.entity.EditorHighlighterEx;
import com.intellij.lang.Language;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.editor.markup.EffectType;
import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.ui.ErrorStripeEditorCustomization;
import com.intellij.ui.LanguageTextField;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.util.ArrayList;

public class MyLanguageTextField extends LanguageTextField {

    private Project myProject;
    private FileType fileType;
    private Language language;
    private EditorEx editorEx;

    private ArrayList<EditorHighlighterEx> highlighter;

    public MyLanguageTextField(Project myProject, Language language, FileType fileType,ArrayList<EditorHighlighterEx> highlighter) {
        super(language, myProject, "", false);
        this.myProject = myProject;
        this.fileType = fileType;
        this.language = language;
        this.highlighter = highlighter;
    }

    @Override
    protected @NotNull EditorEx createEditor() {
        EditorEx editor = super.createEditor();
        setUpEditor(editor);
        return editor;
    }

    @Override
    public void setText(@Nullable String text) {
        super.setFileType(fileType);
        Document document = createDocument(text, language, myProject, new SimpleDocumentCreator());
        setDocument(document);
        PsiFile psiFile = PsiDocumentManager.getInstance(getProject()).getPsiFile(document);
        if (psiFile != null) {
            WriteCommandAction.runWriteCommandAction(
                    getProject(),
                    () -> {
                        CodeStyleManager.getInstance(getProject()).reformat(psiFile);
                    }
            );
        }
    }

    private void setUpEditor(EditorEx editor) {
        /* 偏移到视觉位置 */
        editor.offsetToVisualPosition(100);
        /* 偏移到逻辑位置 */
        editor.offsetToLogicalPosition(100);
        /* 设置水平滚动条可见 */
        editor.setHorizontalScrollbarVisible(true);
        /* 设置聚焦时显示占位符 */
        editor.setShowPlaceholderWhenFocused(true);
        /* 设置垂直滚动条可见 */
        editor.setVerticalScrollbarVisible(true);
        /* 设置脱字符号启用 */
        editor.setCaretEnabled(true);
        /* 设置嵌入对话框包装器 */
        editor.setEmbeddedIntoDialogWrapper(true);

        EditorSettings settings = editor.getSettings();
        /* 设置显示的前导空白 */
        settings.setLeadingWhitespaceShown(true);
        /* 设置显示的尾随空白 */
        settings.setTrailingWhitespaceShown(true);
        /* 设置显示的Gutter图标 */
        settings.setGutterIconsShown(true);
        settings.setSmartHome(true);
        /* 设置显示的行号 */
        settings.setLineNumbersShown(true);
        settings.setIndentGuidesShown(true);
        /* 使用软包装 */
        settings.setUseSoftWraps(true);
        /* 设置自动代码折叠 */
        settings.setAutoCodeFoldingEnabled(true);
        /* 显示折叠轮廓 */
        settings.setFoldingOutlineShown(true);
        /* 设置允许单逻辑线折叠 */
        settings.setAllowSingleLogicalLineFolding(true);
        /* 设置显示的右距 */
        settings.setRightMarginShown(true);
        /* 设置插入行显示 */
        settings.setCaretRowShown(true);
        /* 设置线标记区域显示 */
        settings.setLineMarkerAreaShown(true);
        /* 设置Dnd启用 */
        settings.setDndEnabled(true);
        //开启右侧的错误条纹
        ErrorStripeEditorCustomization.ENABLED.customize(editor);
        /* TODO:(未完成)寻找代码的差异,设置代码高亮 */
//        for(EditorHighlighterEx spanItem:highlighter){
//            setHighlighter(spanItem.getStartOffset(),spanItem.getEndOffset(),editor);
//        }

    }

    public void setMyProject(Project myProject) {
        this.myProject = myProject;
    }

    @Override
    public void setFileType(@NotNull FileType fileType) {
        this.fileType = fileType;
    }

    public void setLanguage(Language language) {
        this.language = language;
    }

    @Override
    public @Nullable EditorEx getEditor() {
        return editorEx;
    }


    /**
     * 设置代码高亮显示
     * */
    public void setHighlighter(Integer startOffset,Integer endOffset,EditorEx editor){
        TextAttributes attributes = new TextAttributes(Color.blue, new Color(184, 198, 234), Color.red, EffectType.SEARCH_MATCH, Font.PLAIN);
        editor.getMarkupModel().addRangeHighlighter(startOffset,
                endOffset,
                HighlighterLayer.SYNTAX - 1,
                attributes,
                HighlighterTargetArea.LINES_IN_RANGE);
    }


}
